package com.navicoa.e_libreria.presentacion;

import com.navicoa.e_libreria.utiles.ValidadorCredencial;
import com.navicoa.e_libreria.utiles.ValidadorCredencialImpl;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.LoginForm.LoginListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class VistaLogin extends AbsoluteLayout{
	
	private Window error = new Window("Error");
	private Label lError2 = new Label();
	private Class<?> claseDestino = VistaMain.class;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public VistaLogin(){
		super();

		this.setImmediate(false);
		this.setWidth("100%");
		this.setHeight("100%");
		this.setMargin(false);
		
		VerticalLayout verticalLayout_1;
		
		verticalLayout_1 = buildVerticalLayout_1();
		this.addComponent(verticalLayout_1);	
		
		ProgressIndicator indicator = new ProgressIndicator(new Float(0.0));
		indicator.addStyleName("actualizador");
		indicator.setPollingInterval(1000);
		
		this.addComponent(indicator);
	}

	private VerticalLayout buildVerticalLayout_1() {
		// common part: create layout
		VerticalLayout verticalLayout_1;
		Embedded embedded_1;
		LoginForm login_1;

		verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("100.0%");
		verticalLayout_1.setHeight("70.0%");
		verticalLayout_1.setMargin(false);
		
		// embedded_1
		embedded_1 = new Embedded();
		embedded_1.setImmediate(false);
		embedded_1.setWidth("-1px");
		embedded_1.setHeight("-1px");
		embedded_1.setSource(new ThemeResource("img/logoELibreria.png"));
		embedded_1.setType(1);
		embedded_1.setMimeType("image/png");
		verticalLayout_1.addComponent(embedded_1);
		verticalLayout_1.setComponentAlignment(embedded_1, new Alignment(24));
		
		// login_1
		login_1 = new LoginForm();
		login_1.setImmediate(false);
		login_1.setWidth("-1px");
		login_1.setHeight("-1px");
		verticalLayout_1.addComponent(login_1);
		verticalLayout_1.setComponentAlignment(login_1, new Alignment(20));
		
		//Ventana de error
		VerticalLayout vLEr = new VerticalLayout();

		Label lError1 = new Label("Error en su Registro");
		
		Button bError = new Button("Cerrar");
		
		bError.addListener(new ClickListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void buttonClick(ClickEvent event) {
				// TODO Apéndice de método generado automáticamente
				(error.getParent()).removeWindow(error);
			}
			
		});

		vLEr.addComponent(lError1);
		vLEr.setComponentAlignment(lError1, Alignment.MIDDLE_CENTER);
		vLEr.addComponent(lError2);
		vLEr.setComponentAlignment(lError2, Alignment.MIDDLE_CENTER);
		vLEr.addComponent(bError);
		vLEr.setComponentAlignment(bError, Alignment.MIDDLE_CENTER);
		
		error.setModal(true);
		
		error.setWidth("300px");
		error.setHeight("200px");
		
		vLEr.setImmediate(false);
		vLEr.setWidth("100%");
		vLEr.setHeight("100%");
		vLEr.setMargin(false);
		vLEr.setSpacing(true);
		
		error.addComponent(vLEr);
		
		error.addStyleName("ventanaError");
		
		login_1.addListener(new LoginListener(){

			/*
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void onLogin(LoginEvent event) {
				ValidadorCredencial vC = new ValidadorCredencialImpl(getApplication(), event, error, lError2, claseDestino);
				vC.validar();
			}
			
		});
		
		return verticalLayout_1;
	}
}
